package com.zhiyou100.action;


import lombok.SneakyThrows;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.net.URLEncoder;

/**
 * @author yang
 * @className Test02
 * @packageName com.zhiyou100.action
 * @Description TODO 文件上传
 * @date 2020-08-24 15:39
 */
@Controller
@RequestMapping("/file")
public class Test02 {

    /**
     * 文件上传
     *
     * @param fileUpload
     * @param request
     * @return
     */
    @SuppressWarnings("all")
    @SneakyThrows
    @RequestMapping("/upload1.action")
    public String fileUpload1(@RequestParam("file") CommonsMultipartFile fileUpload, HttpServletRequest request) {

        // 上传位置保存的位置
        final String path = request.getServletContext().getInitParameter("filePath");
        final File realPath = new File(path);


        // 不存在，则创建目录
        if (!realPath.exists()) {
            realPath.mkdirs();
        }

        // 上传文件地址
        System.out.println("上传文件保存的地址" + realPath.getAbsolutePath());

        // 防止文件名重复，加上毫秒值
        final String filename =
                System.currentTimeMillis() + fileUpload.getOriginalFilename();


        // 最终的地址
        final File finalPath = new File(realPath, filename);

        // 通过 通过CommonsMultipartFile的方法直接写文件
        fileUpload.transferTo(finalPath);

        String message = "上传文件成功！！！\n，文件名 " + fileUpload.getOriginalFilename() + " \n 上传的位置 " + realPath.getAbsolutePath() + "  文件的绝对地址" + finalPath.getAbsolutePath();

        request.setAttribute("message", message);
        return "/index";
    }

    /**
     * 文件下载
     *
     * @param request  request
     * @param response response
     * @param fileName 文件名字
     * @return null
     */
    @SneakyThrows
    @RequestMapping("/download.action")
    public String downloads(HttpServletRequest request, HttpServletResponse response, String fileName) {
        // 获取真实路径
        final String path = request.getServletContext().getInitParameter("filePath");
        //String fileName = "1598257963665deepin-20Beta-desktop-amd64.iso";

        // 设置 response
        response.reset();
        response.setCharacterEncoding("UTF-8");
        response.setContentType("multipart/form-data");
        response.setHeader("Content-Disposition",
                "attachment;fileName=" + URLEncoder.encode(fileName, "UTF-8"));


        final File file = new File(path, fileName);
        final FileInputStream input = new FileInputStream(file);
        final ServletOutputStream out = response.getOutputStream();

        // 拷贝文件
        FileCopyUtils.copy(input, out);


        return null;
    }
}

